import pandas as pd
import os
from datetime import datetime,timedelta,time
import zipfile
import shutil
from tqdm import tqdm
import glob
from dateutil import parser
import numpy as np

def process_date(x):
    date = str(x[0])+' '+x[1]
    date = parser.parse(date)
    if date.time() >= time(18,0,0):
        date = date-timedelta(days=1)
    
    return date

def process_mic(x):
    if pd.isna(x[1]):
        x[0] = x[0]+timedelta(microseconds=100000)
    else:
        if x[0] > x[1]:
            x[0] = x[0]+timedelta(microseconds=100000)
        else:
             x[0] = x[0]+timedelta(microseconds=600000)
    return x[0]

def process_pd(file):
    test = pd.read_csv(file)
    test['datetime'] =  test[['TradingDay','UpdateTime']].apply(process_date,axis=1)
    test['time'] = test['datetime'].apply(lambda x:x.time())
    test = test[((test['time']>=time(9,0,0))&(test['time']<=time(15,0,0)))|((test['time']>=time(21,0,0))&(test['time']<=time(23,0,0)))]
    test['tmp_date'] = test['datetime'].shift()
    test['datetime'] = test[['datetime','tmp_date']].apply(process_mic,axis=1)
    test['datetime'] = pd.to
    test.drop(['TradingDay','UpdateTime','time','LastPrice','tmp_date'],axis=1,inplace=True)
    return test

